package com.xilihui.xlh.business.activitys.store;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.facebook.drawee.view.SimpleDraweeView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.StoreDetailsActivity;
import com.xilihui.xlh.business.dialogs.MapDialog;
import com.xilihui.xlh.business.entities.NearbyStoreEntity;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/9/27.
 */

public class NearbyStoresActivity extends BaseCompatActivity implements OnRefreshListener, OnLoadMoreListener {
    
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    
    ArrayList<NearbyStoreEntity.AgentsBean> data = new ArrayList<>();
    BaseAdapter<NearbyStoreEntity.AgentsBean> baseAdapter;

    String lat = "";
    String lng = "";
    private String agent_id = "";
    int page = 1;

    @Override
    public int getLayout() {
        return R.layout.activity_nearby_stores;
    }

    @Override
    public String initTitle() {
        return "附近门店";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
                
        baseAdapter = new BaseAdapter<NearbyStoreEntity.AgentsBean>(this,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_shop;
            }
            
            @Override
            protected void getView(BaseViewHolder holder, final NearbyStoreEntity.AgentsBean entity, int position) {
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.sdv_shopLogo);
                simpleDraweeView.setImageURI(UrlConst.baseUrl()+entity.getBackground_picture_url());
                
                holder.setText(R.id.tv_shopName,entity.getName());
                holder.setText(R.id.tv_address,entity.getProvince()+entity.getCity()+entity.getDistrict()
                +entity.getAddress()+"|约"+entity.getJuli()+"米");

                String str = entity.getScore();
                if(!TextUtil.isEmpty(str)){
                    float score = Float.parseFloat(str);
                    if(score==0){
                        holder.setVisibility(R.id.ratingBar, View.GONE);
                        holder.setText(R.id.tv_pingfeng,"暂无评分");
                    }else {
                        holder.setVisibility(R.id.ratingBar,View.VISIBLE);
                        holder.setRating(R.id.ratingBar,score);
                        holder.setText(R.id.tv_pingfeng,score+"分");
                    }
                }
                
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        MapDialog mapDialog = new MapDialog(NearbyStoresActivity.this,entity.getLat(),entity.getLng());
                        mapDialog.show();
                    }
                });
                
            }
        };
        
        recyclerView.setAdapter(baseAdapter);
        baseAdapter.setAnimationsLocked(true);
        
        lat = (String) SPUtil.get(this,SPUtil.LAT,"0");
        lng = (String)SPUtil.get(this,SPUtil.LNG,"0");
        agent_id = getIntent().getStringExtra("agent_id");
        
        getData(true);
        
    }

    @Override
    public void initData() {
            
    }
    
    public void getData(boolean isLoading){
        
        StoreRequest.nearbyStores(lat,lng,agent_id,page)
                    .compose(DoTransform.<NearbyStoreEntity>applyScheduler(this,isLoading))
                    .compose(this.<NearbyStoreEntity>bindToLifecycle())
                    .subscribe(new HttpSubscriber<NearbyStoreEntity>(this) {
                        @Override
                        protected void onSuccess(NearbyStoreEntity entity) {
                            
                            if(page==1){
                                data.clear();
                            }
                            
                            data.addAll(entity.getAgents());
                            baseAdapter.setList(data);
                            
                            smartRefreshLayout.finishRefresh();
                            smartRefreshLayout.finishLoadMore();
                            
                            if(page>=entity.getPageCount()){
                                smartRefreshLayout.finishLoadMoreWithNoMoreData();
                            }else {
                                smartRefreshLayout.setNoMoreData(false);
                            }
                            
                        }

                        @Override
                        protected void onFail(String msg) {
                            ToastUtil.toastShortNegative(msg);
                            smartRefreshLayout.finishRefresh();
                            smartRefreshLayout.finishLoadMore();
                        }
                    });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }
    
    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false);
    }
    
}
